<template>
    <div class="goodsinfo-container">
        <transition
                @before-enter="beforeEnter"
                @enter="enter"
                @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag"></div>
        </transition>
        <!--商品轮播图区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :imgs="lunbotu" :isfull="false"></swiper>
                </div>
            </div>
        </div>


        <!--        商品购买区域-->
        <div class="mui-card">
            <div class="mui-card-header">{{goodsinfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:
                        <del class="old">{{goodsinfo.market_price}}</del> &nbsp;&nbsp;
                        销售价: <span class="new">{{goodsinfo.sell_price}}</span>
                    </p>
                    <p class="sell">
                        购买数量:&nbsp;
                        <span class="mui-numbox" data-numbox-min='1' data-numbox-max='9'>
                        <button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
                        <input id="test" class="mui-input-numbox" type="number" value="1"/>
                        <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
                    </span>
                    </p>
                    <mt-button type="danger" size="small">加入购物车</mt-button>
                    <mt-button type="primary" size="small" @click="addToShopCar">立即购买</mt-button>

                </div>
            </div>
        </div>
        <!--        商品参数区域-->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{goodsinfo.goods_no}}</p>
                    <p>库存情况 : {{goodsinfo.stock_quantity}}</p>
                    <p>上架时间：{{goodsinfo.add_time|dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)"> 商品评论</mt-button>

            </div>
        </div>
    </div>
    <!--    分析：商品评论样式
    高度100% 宽自适应  首页中的分歧是宽是100%环视自适应
    方案：只是宽度有分歧：可以定义一个属性，让使用轮播图的调用者，手动指定
    -->
</template>

<script>
    import swiper from '../subcompontents/swiper.vue'

    export default {
        data() {
            return {
                id: this.$route.params.id,
                lunbotu: [],
                goodsinfo: [],
                ballFlag: false //控制小球的显示和隐藏的标识符
            }
        },
        created() {
            this.getLunbotu()
            this.getGoodsInfo();
        },
        methods: {
            getLunbotu() {
                this.$http.get('api/getthumimages/' + this.id).then(result => {
                    console.log(result.body.message);
                    if (result.body.status === 0) {
                        // 先循环轮播图数组每一项，为item添加img属性，应为轮播图组件中，只认识item.img  不认识img.src
                        result.body.message.forEach(item => {
                            item.img = item.src
                        })
                        this.lunbotu = result.body.message;
                    }
                })
            },
            getGoodsInfo() {
                this.$http.get('api/goods/getinfo/' + this.id).then(result => {
                    console.log(result.body.message)
                    if (result.body.status === 0) {
                        this.goodsinfo = result.body.message[0];
                    }
                })
            },
            goDesc(id) {
                // 使用编程式导航，跳转到图文介绍页面
                this.$router.push({name: "goodsdesc", params: {id}})

            },

            goComment(id) {
                this.$router.push({name: "goodscomment", params: {id}})

            },
            addToShopCar() {
                this.ballFlag = !this.ballFlag
            },
            beforeEnter(el) {
                el.style.transform = "translate(0,0)"
            },
            enter(el, done) {
                el.offsetWidth;
                el.style.transform = "translate(521px,202px)"
                el.style.transition = "all 1s ease"
                done();
            },
            afterEnter() {
                this.ballFlag = !this.ballFlag
            }
        },

        components: {
            swiper
        }
    }
</script>

<style lang="less" scoped>
    .goodsinfo-container {
        background-color: #eee;
        /*高度塌陷*/
        overflow: hidden;

        .new {
            font-size: 16px;
            font-weight: bold;
            color: red;
        }

        .mui-card-footer {
            display: block;

            button {
                margin: 15px 0;
            }
        }

        .ball {
            width: 15px;
            height: 15px;
            background-color: red;
            border-radius: 50%;
            position: absolute;
            z-index: 999;
            top: 411px;
            left: 148px;
            transform: translate(410px,100px);
        }
    }

</style>